﻿using System;
using Fee.VendorMatchers;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

namespace Fee.Mapping
{
    public class ImpleVendorMatcherMapping : IEntityTypeConfiguration<ConstructionVendorMatcher>
    {
        public void Configure(EntityTypeBuilder<ConstructionVendorMatcher> builder)
        {
            builder.HasBaseType<VendorMatcher>();
            builder.Property(f => f.IsVip);
            builder.Property(f => f.StationType);
        }
    }

    public class SupervisorVendorMatcherMapping : IEntityTypeConfiguration<SupervisorVendorMatcher>
    {
        public void Configure(EntityTypeBuilder<SupervisorVendorMatcher> builder)
        {
            builder.HasBaseType<VendorMatcher>();
        }
    }

    public class DesignVendorMatcherMapping : IEntityTypeConfiguration<DesignVendorMatcher>
    {
        public void Configure(EntityTypeBuilder<DesignVendorMatcher> builder)
        {
            builder.HasBaseType<VendorMatcher>();
        }
    }

    public class VendorMatcherMapping : IEntityTypeConfiguration<VendorMatcher>
    {
        public void Configure(EntityTypeBuilder<VendorMatcher> builder)
        {
            builder.HasKey(f => f.Id);
            builder.Property(_ => _.Id).ValueGeneratedOnAdd();

            builder.HasOne(f => f.District);
            builder.Property(_ => _.ContractType)
                .HasConversion(
                    v => v.ToString(),
                    v => (ContractType) Enum.Parse(typeof(ContractType), v));
            ;

            builder.HasDiscriminator("discriminator", typeof(string))
                .HasValue<ConstructionVendorMatcher>(ContractType.Construction.ToString())
                .HasValue<DesignVendorMatcher>(ContractType.Design.ToString())
                .HasValue<SupervisorVendorMatcher>(ContractType.Supervisor.ToString());


            builder.HasOne(f => f.Vendor);
            builder.Property(f => f.ConstructionType);
            builder.Property(f => f.Year);

            builder.ToTable("fee_vendorMatcher");
        }
    }
}